

<?php
require_once(dirname(__FILE__).'/../db/studentsDBHandler.php');

if(isset($_GET['id']))
	$student_id=$_GET['id'];
if(isset($_GET['s']))	
	$selected_option=$_GET['s'];
if(isset($_POST['s']))	
	$selected_option=$_POST['s'];
	
$answer="function student(){
		document.getElementById('avi').innerHTML ='We could not find student with the given Id';}";
		
		if(!isset($_POST['s']))
		{
			$find_result = studentsDBHandler::find_student($student_id,$selected_option);
			$num_results = mysql_num_rows($find_result);
		}
		
	/********************************* READING FROM DATABASE *******************************/
	switch($selected_option){
			
	case 0: //Find student personal details
			if($num_results>0)
			{
				/*************************** PREPERING THE RESULT STRING *****************************/				
				//$student_details=$row ['email'];
				$row = mysql_fetch_assoc($find_result);
				$answer="function student(){
				document.getElementById('avi').innerHTML ='First Name: ".$row ['first_name'].
				"<br/>Last Name: ".$row ['last_name'].
				"<br/>Adress: ".$row ['adress'].
				"<br/>Phone: ".$row ['phone'].
				"<br/>Email: ".$row ['email'].
				"';}";
			}
			break;
	case 1: //Find student class details
			if($num_results>0)
			{
				/*************************** PREPERING THE RESULT STRING *****************************/	
				$row = mysql_fetch_assoc($find_result);			
				$answer="function student(){
				document.getElementById('avi').innerHTML ='1. First Class : ".$row ['first_class'].
				"<br/>2. Second Class: ".$row ['second_class'].
				"<br/>3. Third Class: ".$row ['third_class'].
				"';}";
			}
			break;
	case 2: //Add new student 
			
				/*************************** PREPERING THE RESULT STRING *****************************/	
				//add student parameters	
				if(isset($_POST['fName']))	
					$first_name=$_POST['fName'];
				if(isset($_POST['lName']))	
					$last_name=$_POST['lName'];
				if(isset($_POST['Id']))	
					$student_id=$_POST['Id'];
				if(isset($_POST['Adress']))	
					$adress=$_POST['Adress'];
				if(isset($_POST['Phone']))	
					$phone=$_POST['Phone'];
				if(isset($_POST['Email']))	
					$email=$_POST['Email'];
				$object = (object) array('select'=>$_POST['s'],'first_name' => $first_name, 'last_name'=>$last_name, 'id'=>$student_id, 'adress'=>$adress, 'phone'=>$phone , 'email'=>$email);
				$add_result = studentsDBHandler::manage_student($object);
				
				if($add_result)
					$answer="The details successfully added to database";
				else
					$answer="User with the given Id allready exists in database!";
			break;
	case 3: //remove student
				if(isset($_POST['Id']))	
					$student_id=$_POST['Id'];
				$object = (object) array( 'id'=>$student_id, 'select'=>$_POST['s']);
				$add_result = studentsDBHandler::manage_student($object);
				if($add_result)
					$answer="Student successfully removed from database";
				else
					$answer="User with the given ID is not found in database!";
			break;
	case 4: //remove student
				if(isset($_POST['Id']))	
					$student_id=$_POST['Id'];
				if(isset($_POST['optionToUpdate']))	
					$option=$_POST['optionToUpdate'];
				if(isset($_POST['valueToUpdate']))	
					$updated_value=$_POST['valueToUpdate'];
				
				
				$object = (object) array( 'id'=>$student_id, 'select'=>$_POST['s'],'optionToUpdate'=>$option,'valueToUpdate'=>$updated_value);
				$add_result = studentsDBHandler::manage_student($object);
				if($add_result)
					$answer="Student successfully updated database";
				else
					$answer="User with the given ID is not found in database!!!!!";
					
					//$answer=$updated_value."<br/>".$option."<br/> the id is : ".$student_id."<br/>".$_POST['s'];
			break;
	}
	
	
	
	echo  $answer;
		
?>



